<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class BbsController extends CommonController {
	
	public function index(){		
		$bbs=M('bbs_info');

		$key=I('key');
		if(!empty($key)){
			$where['title']=array("like","%".$key."%");
		}
		
		$count = $bbs->where($where)->count();// 查询满足要求的总记录数
		$Page = new \Think\Pager($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page->showhtml();// 分页显示输出
	

		$list=$bbs->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();	
		foreach ($list as $key => $value) {
			$cid=$value['cid'];
			$uid=$value['uid'];
			$categoryInfo=M('category')->where(array('id'=>$cid,'cid'=>1))->find();
			$userInfo=M('user')->where('id='.$uid)->find();

			$list[$key]['category']=$categoryInfo['category'];
			$list[$key]['nickname']=$userInfo['nickname'];
		}
		
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	
	public function manage(){
		$id=I("id");

		$comment=M("bbs_reply");

		$list=$comment->where('bbs_id='.$id)->select();

		foreach ($list as $key => $value) {
			$uid=$value['uid'];
			$userInfo=M('user')->where('id='.$uid)->find();
			$list[$key]['nickname']=$userInfo['nickname'];
			$list[$key]['content']=base64_decode($value['content']);
		}

		$this->assign('list',$list);
		$this->display();
	}

	//删除
	public function del(){
		$id=I('id');

		$delInfo=M('bbs_info')->where('id='.$id)->delete();

		if($delInfo){
			$this->ajaxReturn(array('s'=>true,'m'=>"删除成功!"));
		}else{
			$this->ajaxReturn(array('s'=>false,'m'=>"删除失败!"));
		}
	}

}